package com.zeei.ems.base.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zeei.ems.base.constant.GlobalConstant;
import com.zeei.ems.base.dao.ConfigurationManagementDao;
import com.zeei.ems.base.service.BasicServiceImpl;
import com.zeei.ems.base.service.ConfigurationManagementService;
import com.zeei.ems.base.vo.GeneralCategoryVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;

@Service("configurationManagementService")
public class ConfigurationManagementServiceImpl extends BasicServiceImpl implements ConfigurationManagementService {
	@Autowired
	ConfigurationManagementDao configurationManagementDao;

	@Override
	public PageInfo<GeneralCategoryVO> dictionaryManagement(GeneralCategoryVO generalCategoryParam) {
		this.setPage(generalCategoryParam.getPage(), generalCategoryParam.getRows());
		List<GeneralCategoryVO> list = configurationManagementDao.queryDictionaryMessage(generalCategoryParam);
		return new PageInfo<>(list);
	}

	@Override
	public boolean editDictionary(GeneralCategoryVO generalCategoryParam, String oldGeneralCode) {
		if (StringUtils.isEmpty(oldGeneralCode)) {
			Integer count = configurationManagementDao.updateDictionaryByGeneralCode(generalCategoryParam);
			return count > 0;
		} else {
			Integer count = configurationManagementDao.dropDictionary(oldGeneralCode);
			count += configurationManagementDao.addDictionary(generalCategoryParam);
			return count > 1;
		}
	}

	@Override
	public boolean addDictionary(GeneralCategoryVO generalCategoryParam) {
		Integer count = configurationManagementDao.addDictionary(generalCategoryParam);
		return count > 0;
	}

	@Override
	public List<GeneralCategoryVO> getCategoryList(String categoryclass,Integer systemType,int type) {
		Map<String,Object> map = new HashMap<>(16);
		map.put("categoryClass",categoryclass);
		map.put("systemType",systemType);
		map.put("type", type);
		return configurationManagementDao.getCategoryList(map);
	}

	@Override
	public boolean addDictionaryType(GeneralCategoryVO generalCategoryParam) {
		Integer count = configurationManagementDao.addDictionaryType(generalCategoryParam);
		return count > 0;
	}

	@Override
	public boolean dropDictionary(List<String> list) {
		Integer count = configurationManagementDao.dropDictionaryList(list);
		return count > 0;
	}


	@Override
	public List<String> getAccessUrl() {
		List<String> urlList = configurationManagementDao.getAccessUrl();
		return urlList;
	}
	
	@Override
	public boolean validGeneralCode(String generalCode) {
		if (StringUtils.isEmpty(generalCode)) {
			return GlobalConstant.FAILED;// 失败
		}
		Map<String, Object> condition = new HashMap<>();
		condition.put("generalCode", generalCode);
		List<GeneralCategoryVO> list = configurationManagementDao.queryDictionaryMessageByCondition(condition);
		if (list.isEmpty()) {
			return GlobalConstant.SUCCEED;// 成功
		}
		return GlobalConstant.FAILED;// 失败
	}

	@Override
	public boolean validCategoryClass(String categoryClass) {
		if (StringUtils.isEmpty(categoryClass)) {
			return GlobalConstant.FAILED;// 失败
		}
		GeneralCategoryVO generalCategoryParam = new GeneralCategoryVO();
		generalCategoryParam.setCategoryClass(categoryClass);
		List<GeneralCategoryVO> list = configurationManagementDao.getCategoryClass(generalCategoryParam);
		if (CollectionUtils.isEmpty(list)) {
			return GlobalConstant.SUCCEED;// 成功
		}
		return GlobalConstant.FAILED;// 失败
	}

	@Override
	public boolean validCategoryName(String categoryName) {
		if (StringUtils.isEmpty(categoryName)) {
			return GlobalConstant.FAILED;// 失败
		}
		GeneralCategoryVO generalCategoryParam = new GeneralCategoryVO();
		generalCategoryParam.setCategoryName(categoryName);
		List<GeneralCategoryVO> list = configurationManagementDao.getCategoryName(generalCategoryParam);
		if (CollectionUtils.isEmpty(list)) {
			return GlobalConstant.SUCCEED;// 成功
		}
		return GlobalConstant.FAILED;// 失败
	}
	
	@Override
	public List<GeneralCategoryVO> getCategoryClass() {
		return configurationManagementDao.getCategoryClass(new GeneralCategoryVO());
	}
}
